package cn.tedu._10article.controller;

import cn.tedu._10article.base.response.Result;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author tune_the last
 * @date 2025/11/19 - 15:35
 */
@Slf4j
@Tag(name = "文件上传")
@RestController
@RequestMapping("/v1/file")
public class UploadController {
    @Operation(summary = "文件上传功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("/upload")
    public Result UploadController(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.debug("文件后缀名为: " + suffix);
        String filename = UUID.randomUUID() +suffix;
        String dirPath = "D:/ideaWorkSpace2/files/";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        File f = new File(dirPath + datePath);
        if (!f.exists()) {
            f.mkdirs();
        }
        String filePath = dirPath + datePath + filename;
        log.debug("filePath: " + filePath);
        file.transferTo(new File(filePath));
        return Result.ok();
    }
}
